var _testerId = 0;
var tenantList = [];
var tenantMap = {};
var productList = [];
var productMap = {};
var currentTenant = {};
var currentProduct = {};
var tenantIndex = 0;
var productIndex = 0;


function getTenant(testerId) {
    _testerId = testerId;
    layer.open({type: 2});
    $.ajax({

        type: "post",

        url: common.getTenantFromUserUrl,

        data: {"testerId": testerId},

        success: function (result) {
            //这里是解析的json数据   直接返回data
            var datas = result;
            if (datas.code == 0) {
                var list = datas.data;
                var str = '<option value="">请选择</option>';
                for (var i = 0; i < list.length; i++) {
                    var data = {};
                    var o = list[i];
                    data.label = o.tenantName;
                    data.value = o.tenantId;
                    tenantList.push(data);
                    tenantMap[data.value] = o;
                    if (i == 0) {
                        tenantIndex = data.value;
                    }
                }
            } else {
                common.showMsgTips(datas.msg);
            }

            layer.closeAll();
        }, error: function () {
            layer.closeAll();
        }

    });
}


(function () {
    //loading层
    // getTenant(1);

    $('#sg-tenant').on('click', function (e) {
        weui.picker(tenantList, {
            defaultValue: [tenantIndex],
            onChange: function (result) {
                // console.log(result);
            },
            onConfirm: function (result) {
                currentTenant = tenantMap[result[0]];
                $('#sg-tenant').val(currentTenant.tenantName);
                productList = [];
                productMap = {};
                currentProduct = {};
                $('#sg-product').val("请选择");
                weui.picker([]);
                //loading层
                layer.open({type: 2});
                $.ajax({
                    type: "post",
                    url: common.getAllProductsNameIdUrl,
                    data: {"tenantID":currentTenant.tenantId},
                    dataType: 'json',
                    success: function (result) {
                        //这里是解析的json数据   直接返回data
                        var data = result;
                        var str = '<option value="">请选择</option>';
                        if (data.code == 0) {
                            var list = data.data.listData;
                            for (var i = 0; i < list.length; i++) {
                                var o = list[i];
                                var _o = {};
                                _o.label = o.productName;
                                _o.value = o.productID;
                                str += '<option value="' + o.productID + '" deviceTypeId="' + o.deviceTypeID + '">' + o.productName + '</option>';// deviceTypeID
                                productList.push(_o);
                                productMap[o.productID] = o;
                                if (i == 0) {
                                    productIndex = o.productID;
                                }
                            }
                        } else {
                            common.showMsgTips(data.msg);
                        }
                        $('#sg-product').html(str);
                        layer.closeAll();
                    }, error: function () {
                        layer.closeAll();
                    }

                });
            }
        });
    });
    $('#sg-product').on('click', function () {
        weui.picker(productList, {
            defaultValue: [productIndex],
            onChange: function (result) {
                // console.log(result);
            },
            onConfirm: function (result) {
                currentProduct = productMap[result[0]];
                $('#sg-product').val(currentProduct.productName);
            }
        });
    });

    /* $("#sg-tenant").change(function () {
         $.ajax({

             type: "post",

             url: common.getAllProductsNameIdUrl,

             data: {"tenantID": $(this).val()},

             dataType: 'json',
             success: function (result) {
                 //这里是解析的json数据   直接返回data
                 var data = result;
                 console.log(data);
                 var str = '<option value="">请选择</option>';
                 if (data.code == 0) {
                     var list = data.data.listData;
                     for (var i = 0; i < list.length; i++) {
                         var o = list[i];
                         str += '<option value="' + o.productID + '" deviceTypeId="'+o.deviceTypeID+'">' + o.productName + '</option>';// deviceTypeID
                     }
                 } else {
                     common.showMsgTips(data.msg);
                 }
                 $('#sg-product').html(str);
             }

         });
     });*/
})();

function submit() {
    // $('#sg-tenant').html(str);//目标租户
    // $('#sg-product').html(str);// 目标产品
    var obj = {};
    obj.tenantId = currentTenant.tenantId;//租户id
    obj.tenantName = currentTenant.tenantName;//租户名称
    obj.productId = currentProduct.productID;//产品id
    obj.productName = currentProduct.productName;//产品名称
    obj.deviceTypeID = currentProduct.deviceTypeID;//产品名称

    if (obj.tenantId == null || obj.tenantId == "") {
        common.errorTips("请选择租户");
        return false;
    }

    if (obj.productId == null || obj.productId == "") {
        common.errorTips("请选择产品");
        return false;
    }
    $.ajax({
        type: "post",
        url: common.getConfigByIdUrl,
        data:
            {
                "produceId": obj.productId,
                "testUserId": _testerId,
                "testTenantId": obj.tenantId
            },

        dataType: 'json',
        success: function (result) {
            //这里是解析的json数据   直接返回data
            var data = result;
            if (data.code == 0) {
                if (data.data == 0) {
                    window.wa.confirmTenant(JSON.stringify(obj), 3);
                }else{
                    window.wa.confirmTenant(JSON.stringify(obj),data.data);
                }
            } else {
                common.showMsgTips(data.msg);
            }
            layer.closeAll();
        }, error: function () {
            layer.closeAll();
        }

    });

}

